package listbyorder.access001_100.test98;

import listbyorder.utils.TreeNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/6/6 10:42
 */
public class Solution3 {

    // 方法三： 使用简单的递归方式进行求解
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        return getAns(root, null, null);
    }

    private boolean getAns(TreeNode root, Integer lower, Integer upper) {
        if (root == null) return true;

        if (lower != null && root.val <= lower) return false;
        if (upper != null && root.val >= upper) return false;

        if (!getAns(root.left, lower, root.val)) return false;
        if (!getAns(root.right, root.val, upper)) return false;

        return true;
    }
}
